﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WFNetLib;
using System.Windows.Forms;
namespace RobotControl
{
    public partial class Form_Main : Form
    {
        public byte JianCeRxCount = 0;
        Union_UInt16 TValue;
        Union_UInt16 FValue;
        private void JianCeCom_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            byte rx;
            if (ComClosing[2])
                return;
            ComListening[2] = true;
            while (JianCeCom.BytesToRead != 0)
            {
                int irx = JianCeCom.ReadByte();
                if (irx == -1)
                    break;
                rx = (byte)irx;
                JianCeRxCount++;
                switch (JianCeRxCount)
                {
                    case 1:
                        TValue.ofs1_8 = rx;
                        break;
                    case 2:
                        TValue.ofs0_8 = rx;
                        break;
                    case 3:
                        FValue.ofs1_8 = rx;
                        break;
                    case 4:
                        FValue.ofs0_8 = rx;
                        break;
                    case 5:
                        float t = Convert.ToSingle(TValue.ofs_16);                        
                        t = t / 10;
                        ExternChangeParamList(ParamsListAddr.温度, t.ToString("F1"));
                        ExternChangeParamList(ParamsListAddr.频率, FValue.ofs_16.ToString());
                        if (rx == 0x01)
                        {
                            ExternChangeParamList(ParamsListAddr.漏水, "是");
                            WFNetLib.WinAPI.Win32API.Beep(500, 100);
                        }
                        else
                            ExternChangeParamList(ParamsListAddr.漏水, "否");
                        string s;
                        if (rx == 0x01)
                            s="漏水";
                        else
                            s="未漏水";
                        ExternAddText("收到磁力仪舱检测数据:\r\n" + "温度:" + t.ToString("F1") + "\r\n" + "频率:" + FValue.ofs_16.ToString()+"\r\n"+s);
                        
                        JianCeRxCount = 0;
                        break;
                    default:
                        JianCeRxCount = 0;
                        break;
                }
            }
            ComListening[2] = false;
        } 
    }
}
